﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace UI.Admin.Column
{
    /// <summary>
    /// AjaxAction 的摘要说明
    /// </summary>
    public class AjaxAction : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            // 验证
            //string auth = Utility.Authorization.Check(HttpContext.Current.Session["role"], "ColumnAdmin", "");
            //if (auth != "ACCEPT")
           // {
           //     return;
           // }

            context.Response.ContentType = "text/plain";

            string dowhat = context.Request["dowhat"];
            if (dowhat == null) dowhat = "";

            // 操作用户及IP
            string UserName = "test";//((Models.User)(HttpContext.Current.Session["user"])).Name;
            string UserIP = "1314";//context.Request.ServerVariables["LOCAl_ADDR"];

            #region 添加栏目
            if (dowhat == "add_column")
            {
                string ParentID = context.Request["ParentID"];
                int SumClass = 2;
                for (int i = 0; i < 3; i++) {
                    if (ParentID != "0")
                    {
                        SumClass += 1;
                        ParentID = BLL.Find.FindOneColumn(ParentID).ParentID.ToString();
                    }
                }
                if (SumClass > 4)
                {
                    context.Response.Write("最多支持四级菜单."); return;
                }
                else{ParentID = context.Request["ParentID"];}
                string Name = context.Request["Name"];
                string ModelID = context.Request["ModelID"];
                string ColumnLogo = context.Request["ColumnLogo"];
                string Remark = context.Request["Remark"];

                if (BLL.Create.InsertColumn(Name, ParentID, ModelID,ColumnLogo, Remark) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "添加了‘" + Name + "’栏目");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("faild");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 更新栏目
            if (dowhat == "update_column")
            {
                string ID = context.Request["ID"];
                string ParentID = context.Request["ParentID"];
                string ModelID = context.Request["ModelID"];
                if (ID == ParentID) { context.Response.Write("栏目本身不能作为父级栏目。"); return; }
                string Name = context.Request["Name"];
                string ColumnLogo = context.Request["ColumnLogo"];
                string Remark = context.Request["Remark"];

                if (BLL.Update.UpdateColumn(ID, Name, ParentID, ModelID, ColumnLogo,Remark) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "更新了‘" + Name + "’栏目");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("除了备注，所有信息不能为空");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 删除栏目
            if (dowhat == "delete_column")
            {
                string ID = context.Request["ID"];

                string Name = BLL.Find.FindOneColumn(ID).Name;

                if (BLL.Delete.DeleteColumn(ID) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "删除了‘" + Name + "’栏目");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("faild");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 排序栏目
            if (dowhat == "sort_column")
            {
                string OrderID = context.Request["OrderID"];
                string ID = context.Request["ID"];
                string ParentID = context.Request["ParentID"];
                if (BLL.Update.UpdateSortColumn(ID, ParentID, OrderID) > 0)
                {
                    context.Response.Write("success");
                }
                else
                {
                    context.Response.Write("faild");
                }
                context.Response.End();
                return;
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}